/*************************************************************************
	> File Name: 010.P1364_医院设置.cpp
	> Author: Maureen 
	> Mail: Maureen@qq.com 
	> Created Time: 三  6/23 16:21:31 2021
 ************************************************************************/

#include <iostream>
#include <cstring>
using namespace std;

//www.luogu.com.cn
//Floyd算法解决
int n, arr[105][105], num[105]; //n为节点数，arr为邻接矩阵，num为每个点的人数

int main() {
    memset(arr, 0x3f, sizeof(arr));
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int a, b; //a左孩子，b右孩子
        cin >> num[i] >> a >> b; 
        if (a != 0) { //a和i是相连的
            arr[i][a] = 1;
            arr[a][i] = 1;
        }
        if (b != 0) {
            arr[i][b] = 1;
            arr[b][i] = 1;
        }
        arr[i][i] = 0; //自己到自己为0
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            for (int k = 1; k <= n; k++) {
                arr[j][k] = min(arr[j][k], arr[j][i] + arr[i][k]);
            }
        }
    }

    //枚举点
    int ans = 999999999;
    for (int i = 1; i <= n; i++) {
        int t = 0; //第i个点设置为医院时的总距离
        for (int j = 1; j <= n; j++) {
            t += num[j] * arr[j][i]; //j号点走到i号点的距离
        }
        ans = min(ans, t);
    }
    cout << ans << endl;

    return 0;
}

